from django.shortcuts import render, HttpResponse, redirect
from myApp.models import UserInfo, Department

# Create your views here.
def index(request):
    return HttpResponse("欢迎使用")

def user_list(request):
    # render函数会
    # 根据注册顺序，逐一从
    # app目录下的templates目录下寻找html文件
    return render(request, "user_list.html")

def tql(request):
    writer = "季越人"
    roles = ['长湖', '通崖', '项平', '尺泾']
    type = {'伯':'长湖', '仲':'通崖', '叔':'项平', '季':'尺泾'}

    return render(request, "tql.html", {"n1":writer, "n2":roles, "n3":type})

def something(request):
    # request 是一个对象，封装了用户发送过来的所有请求相关数据

    # 1. [请求] 获取请求方式
    print("用户请求的方式: " + request.method)

    # 2. [请求] 在URL上传递值，例如 例如: http://123.249.26.154:5900/something/?n1=1&n2=2
    print(request.GET)

    # 3. [请求]在请求体中提交数据，目前是空值
    print(request.POST)

    # 4. [响应]HttpResponse("返回内容"),内容字符串内容返回给请求者
    return HttpResponse("返回内容")

    # 5. [响应]读取HTML的内容 + 渲染(替换) => 最终都转化为字符串返回给用户浏览器
    return render(request, 'something.html', {"title":"Hello"})

    # 6. [响应] 让浏览器重定向到其他的页面
    return redirect("http://www.baidu.com")

def login(request):
    if request.method == 'GET':
        print("GET")
        # 初始的时候，用户浏览器发送过来的请求是GET
        return render(request, '案例：用户登录.html')
        print("GET")
    else:
        # 如果表单中的数据提交，则用户浏览器请求方式是POST
        print(request.POST)
        # 通过html中设置的name属性获取值
        username = request.POST.get('user')
        password = request.POST.get('pwd')
        print("Username: ",username)
        print("Pwd: ", password)
        if username=='joker' and password=='123':
            return HttpResponse("登录成功")
        else:
            return render(request, '案例：用户登录.html', {"error_msg": "用户名或密码错误"})


def info_list(request):
    # 获取表中所有数据
    data_list = UserInfo.objects.all()

    return render(request, 'info_list.html', {"data_list":data_list})

def info_add(request):
    if request.method == 'GET':
        return render(request, 'info_add.html')
    else:
        # 获取用户提交的数据
        name = request.POST.get("name")
        password = request.POST.get("pwd")
        age = request.POST.get("age")

        # 如果name或者password或者age有一个为空，就无法加入到数据库里面
        if name=='' or password =='' or age=='':
            err_msgs = ['用户名不能为空', '密码不能为空', '年龄不能为空']
            name_err, pwd_err, age_err = '', '', ''
            if name == '':
                name_err = err_msgs[0]
            if password == '':
                pwd_err = err_msgs[1]
            if age == '':
                age_err = err_msgs[2]
            return render(request, 'info_add.html', {"name_err":name_err, "pwd_err":pwd_err, "age_err":age_err})
        # 添加到数据库
        UserInfo.objects.create(name=name, password=password, age = age)

        # 自动跳转
        return redirect("/info/list/")
    
def info_delete(request):
    
    nid = request.GET.get("nid")
    UserInfo.objects.filter(id=nid).delete()
    return redirect("/info/list/")

def depart_list(request):
    """部门列表"""
    depart_list = Department.objects.all()

    return render(request, "depart_list.html", {"depart_list": depart_list})

def depart_add(request):
    """添加部门"""
    if request.method == "GET":
        return render(request, "depart_add.html")
    else:
        # 获取用户提交的部门数据
        depart_title = request.POST.get("depart_title")
        
        # 保存用户提交的部门数据
        Department.objects.create(title=depart_title)

        return redirect("/depart/list/")

def depart_delete(request):
    """部门删除"""

    nid = request.GET.get('nid')
    Department.objects.filter(id=nid).delete()

    # 重定向回部门列表
    return redirect('/depart/list/')

def depart_edit(request, nid):
    """部门编辑"""
    if request.method=="GET":
        # 根据nid获取数据
        row_object = Department.objects.filter(id=nid).first()
        return render(request, 'depart_edit.html', {"row_object":row_object})
    else:
        # 如果是POST请求保存修改
        # 获取 用户修改后的title
        depart_title = request.POST.get('depart_title')
        # 更新title
        Department.objects.filter(id=nid).update(title=depart_title)

        # 重定向回部门列表
        return redirect('/depart/list/')